package Controler;

import java.util.ArrayList;

import Client.BusinessImplements;
/**
*  @author Lucie
 *class Return_Book_controler
 *Class variables:msg,flag
 *Class methods:get_data,check_input,get_msg,set_msg
 */
public class Return_Book_controler {
	
	private String msg; 
	private boolean flag;
	/**
	 * Constructor initializes the class variables
	 */
	public Return_Book_controler()
	{
		flag=true;
		msg=new String();
		msg=null;
	}
	/**
	 * 
	 * @return String white all of the problems that occurred in the class
	 */
	public String get_msg()
	{
		return msg;
	}
	/**
	 *  @param new_msg
	 * This method chain messages into a string ,
	 *  the purpose ultimately is to display on the screen all the failures
	 */
	public void set_msg(String new_msg)
	{
		if (msg==null)
			msg=new_msg;
		else
		msg+=new_msg;
		
	}
	
	/**
	 * 
	 * @param copy_number
	* this  method call the class ;serverproxy, to Contact with the server,
	 * first we check the Parameters we get (check_input) if something is wrong we return false 
	 * then we check if the item exist and if he is on lend with the method "checkItemCopyStatus"
	 * that return "copynotexist" if the copy not exist,"copynotonlend" if the copy status is not on lend
	 * or "ok" if everything is find, if the method return "ok" we call the method "add_ItemReturn" that
	 * return the book and change his status in the data base
	 *  @return  boolean:If the calls in the method failed it returns false otherwise returns true
	 */
	public boolean get_data(String copy_number)
	{
		 ArrayList<String> str_meember_status= new  ArrayList<String>();
		 ArrayList<String> str_result=new  ArrayList<String>();
		
		 
		 check_input( copy_number);
		 if (flag==false)
			 return false;
		 
		
		 str_meember_status=BusinessImplements.GetInstance().getServerProxy().checkItemCopyStatus(copy_number);
		
		
		 if (str_meember_status.get(0).equals("copynotexist"))
		 {
			 set_msg("\ncopy number not found\nplease try again"); 
			 return false;
		 }
	
		 if (str_meember_status.get(0).equals("copynotonlend"))
		 {
			 set_msg("\nthe copy is not in lend\n therefor there is not a possibility to return "); 
			 return false;
		 }
		
		 if (str_meember_status.get(0).equals("ok"))
		 {
		 str_result=BusinessImplements.GetInstance().getServerProxy().add_ItemReturn(copy_number);
		 if (str_result.get(0).equals("false"))
		 {
			 set_msg("\nmemmber return book faild"); 
			 return false;
		 }
		 }
		 else
		 {
			 set_msg("\nexception occur.");
			 return false;
		 }

			return flag;
	}
	
/**
 * This method checks the parameters we have received are not empty
 * if they are poses flag= false;
 * @param copy_id
 */
public void check_input(String copy_id)
{
			
	if(copy_id.equals(""))
		{
			set_msg("\nmising copy id ");
			flag=false;
		}
		
}
	

	

}

	 


